package lan.battletank.element;

import java.util.Random;

import lan.battletank.R;
import lan.battletank.environment.EnvConst;
import lan.battletank.main.GameStatus;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;

public class IndicatorEntity {

    private Context mContext;

    private final int BAR_WIDTH = 100;	// width of the bar(s)
    private final int BAR_HEIGHT = 10;	// height of the bar(s)
    private final int MARGIN = 5;
    private final int POWER_INCREASEMNET = 25;	// fire power increament per second
    private final int DISPLAY_DURATION = 30;	// message display last for 30 frames
    
    private int mCanvasWidth;
    private int mCanvasHeight;

    /** Wind indicator image. */
    private Bitmap mWindLeftOrg;
    private Bitmap mWindRightOrg;
    private Bitmap mWindLeft;
    private Bitmap mWindRight;

    /** Wind power. */
    private int mWindPower;

    /** Paint to draw the fire power indicator on screen. */
    private Paint mFirePowerPaint;
    private Paint mFirePowerMaxPaint;

    /** Scratch rect object. */
    private RectF mScratchRect;

    /** The FIRE Button is pressed. */
    private boolean mShowFirePower;

    /** The power depends on how long the FIRE button is pressed. */
    private double mFirePower;

    private Paint pNormal = new Paint();
    private Paint pWin = new Paint();
    private Paint pLose = new Paint();

    /** Counter used for count the duration of display message */
    private int mCounter;

    /** Constructor, initial all parameters here. */
    public IndicatorEntity(Context context) {
        mContext = context;
        Resources res = mContext.getResources();
        mWindLeftOrg = BitmapFactory.decodeResource(res, R.drawable.wind_left);
        mWindRightOrg = BitmapFactory.decodeResource(res, R.drawable.wind_right);

        mFirePowerPaint = new Paint();
        mFirePowerPaint.setAntiAlias(true);
        mFirePowerPaint.setARGB(255, 255, 0, 0);
        mFirePowerMaxPaint = new Paint();
        mFirePowerMaxPaint.setAntiAlias(true);
        mFirePowerMaxPaint.setARGB(200, 0, 255, 0);
        pNormal.setColor(Color.BLUE);
        pNormal.setStyle(Style.FILL);
        pNormal.setTextSize(40);
        pWin.setColor(Color.GREEN);
        pWin.setStyle(Style.FILL);
        pWin.setTextSize(40);
        pLose.setColor(Color.RED);
        pLose.setStyle(Style.FILL);
        pLose.setTextSize(40);
        mScratchRect = new RectF(0, 0, 0, 0);

        mShowFirePower = false;
        mFirePower = 0;
        mCounter = 0;
        mWindPower = 0;
    }

    public void draw(Canvas c) {
        mScratchRect.set(MARGIN, MARGIN, MARGIN + BAR_WIDTH, MARGIN + BAR_HEIGHT);
        c.drawRect(mScratchRect, mFirePowerMaxPaint);
        mScratchRect.set(MARGIN, MARGIN - 1, MARGIN + (int) mFirePower, MARGIN + BAR_HEIGHT);
        c.drawRect(mScratchRect, mFirePowerPaint);
        if (GameStatus.getGameStatus() == EnvConst.GAME_WIN) {
            c.drawText("YOU WIN!", 200, 100, pWin);
            c.drawText("Tap the screen to start a new round", 100, 150, pWin);
        } else if (GameStatus.getGameStatus() == EnvConst.GAME_LOSE) {
            c.drawText("YOU LOSE!", 200, 100, pLose);
            c.drawText("Tap the screen to start a new round", 100, 150, pLose);
        } else if (mCounter < DISPLAY_DURATION) {
            if (GameStatus.getBattleStatus() == EnvConst.BATTLE_PLAYER_TURN) {
                c.drawText("PLAYER TURN", 200, 100, pNormal);
            } else if (GameStatus.getBattleStatus() == EnvConst.BATTLE_PLAYER_TURN_END) {
                c.drawText("PLAYER TURN END", 200, 100, pNormal);
            } else if (GameStatus.getBattleStatus() == EnvConst.BATTLE_ENEMY_TURN) {
                c.drawText("ENEMY TURN", 200, 100, pNormal);
            } else if (GameStatus.getBattleStatus() == EnvConst.BATTLE_ENEMY_TURN_END) {
                c.drawText("ENEMY TURN END", 200, 100, pNormal);
            }
        }
        if (mWindPower > 0) {
            c.drawBitmap(mWindLeft, mCanvasWidth - MARGIN - mWindPower, MARGIN, null);
        } else if (mWindPower < 0) {
            c.drawBitmap(mWindRight, mCanvasWidth - MARGIN + mWindPower, MARGIN, null);
        }
    }

    public void update(double timeInterval) {
        if (mShowFirePower && mFirePower < BAR_WIDTH) {
            mFirePower += timeInterval * POWER_INCREASEMNET;
        } else if (mFirePower > BAR_WIDTH) {
            mFirePower = BAR_WIDTH;
        }
        if (mCounter < DISPLAY_DURATION) {
            mCounter++;
        }
    }

    public void setPositionAndInitialize(int canvasWidth, int canvasHeight) {
        mCanvasWidth = canvasWidth;
        mCanvasHeight = canvasHeight;
    }

    public void setChangingFirePower(boolean b) {
        mShowFirePower = b;
    }

    public boolean isChangingFirePower() {
        return mShowFirePower;
    }

    public void clearFireIndicator() {
        mFirePower = 0;
    }

    public int getFirePower() {
        return (int) mFirePower;
    }

    public void setGameStatus(int gameStatus) {
        switch (gameStatus) {
        case EnvConst.GAME_IN_BATTLE:
            break;
        case EnvConst.GAME_WIN:
            break;
        case EnvConst.GAME_LOSE:
            break;
        case EnvConst.GAME_PAUSE:
            break;
        }
    }

    public void setBattleStatus(int battleStatus) {
        switch (battleStatus) {
        case EnvConst.BATTLE_PLAYER_TURN:
            determineWindPower();
            mCounter = 0;
            if (mWindPower > 0) {
                mWindLeft = Bitmap.createScaledBitmap(mWindLeftOrg, mWindPower, 20, true);
            } else if (mWindPower < 0) {
                mWindRight = Bitmap.createScaledBitmap(mWindRightOrg, -mWindPower, 20, true);
            }

            break;
        case EnvConst.BATTLE_PLAYER_TURN_END:
            mCounter = 0;
            break;
        case EnvConst.BATTLE_ENEMY_TURN:
            mCounter = 0;
            break;
        case EnvConst.BATTLE_ENEMY_TURN_END:
            mCounter = 0;
            break;
        }
    }

    public boolean isShowingMessage() {
        return mCounter < DISPLAY_DURATION;
    }

    public int getWindPower() {
        return mWindPower;
    }

    private void determineWindPower() {
        Random rand = new Random();
        mWindPower = (int) ((EnvConst.WIND_LEVELS + 1) * rand.nextFloat()) * EnvConst.WIND_GRADE * (rand.nextBoolean() ? -1 : 1);
    }
}
